#pragma once

#include "pathBuilder.hpp"

#include <godot_cpp/classes/shape_cast3d.hpp>
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/box_shape3d.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/physics_direct_space_state3d.hpp>
#include <godot_cpp/classes/physics_shape_query_parameters3d.hpp>
#include <stack>

using namespace godot;

class SpaceNodeHandler : public ISpaceBuilderHandler
{
public:
    SpaceNodeHandler( PhysicsDirectSpaceState3D* world );
    bool ValidCond( const BuilderNode* vol ) override;
    void SplitCallback( const bool cond, BuilderNode* vol ) override;
    void LeafCallback( const bool cond, BuilderNode* vol ) override;
private:
    PhysicsDirectSpaceState3D* m_world;
};

class ExPathBuilder : public Node3D {
    GDCLASS(ExPathBuilder, Node3D)
public:
    void _enter_tree() override;

protected:
    static void _bind_methods();

    void Init( PhysicsDirectSpaceState3D* world );
    void BuildSpaceInfo( Vector3 volume, float minsize, Vector3 selectPt);
    void ExportSpaceInfo( String fPath );
    void ImportSpaceInfo( String fPath );
    void FillPathAreas( Array centers, Array sizes );
    void FillConnectionsLines( Array line );
    void FetchVolumeInfo( Dictionary info );
    void FindPath( Vector3 s, Vector3 e, float sa, Array waypoints);
private:
    void M_PushConnectionLine( Voxel& v0, Voxel& v1, Array& line );
    BuilderRoot m_builder;
    SpaceRoot m_space;
    SpaceNodeHandler* m_testHandler = nullptr;

    Array m_centers;
    Array m_sizes;
};
